/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MatLab;

import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;

/**
 *
 * @author Usuario
 */
public class MatLabComparacion {

    private String commands_part1 = ""
            + "close all\n"
            + "clear all\n"
            + "clc\n"
            + "\n"
            + "cd('";
    private String commands_part2 = ""
            + "')\n"
            + "param_gamma     = load('";
    private String commands_part3 = ""
            + "');\n"
            + "nombre_m_ref    = ['dcm0' num2str(param_gamma(1))];\n"
            + "nombre_m_dat    = ['729_QA_' num2str(param_gamma(1)) '.dcm'];\n"
            + "\n"
            + "I1_aux  =  dicomread(nombre_m_ref);          % matriz de referencia (dosis medida o calc con Cat3D)\n"
            + "I1      =  I1_aux(150:250,150:250);\n"
            + "I2_aux  =  dicomread(nombre_m_dat);   % matriz de datos      (calculada con matlab)\n"
            + "I2      =  I2_aux(100:200,100:200);\n"
            + "I1      =  I1*468;\n"
            + "A1      =  im2double(I1);\n"
            + "A2      =  im2double(I2);\n"
            + "DTA     =  param_gamma(2);                           % en mm\n"
            + "dosed   =  param_gamma(3);                          % en %\n"
            + "umbral  =  param_gamma(4);                          % en %\n"
            + "umbral  =  umbral/103;\n"
            + "\n"
            + "size1   = size (A1) ;\n"
            + "size2   = size (A2) ;\n"
            + "dosed   = dosed *  max(A1 ( : ) ) ;   % escalo la dosis como un porcentaje de la máxima dosis\n"
            + "umbral = umbral* max(A1 ( : ) ); \n"
            + "A2 = A2*max(A1 ( : ) )/max(A2 ( : ) );   %hago analisis relativo\n"
            + "cont_NO = 0;\n"
            + "for i = 1 : size1( 1 )\n"
            + "            for j = 1 : size1( 2 )\n"
            + "                if A1(i,j) < umbral,\n"
            + "                    A1(i,j) = 0;\n"
            + "                    A2(i,j) = 0;\n"
            + "                    cont_NO = cont_NO + 1; \n"
            + "                end\n"
            + "            end\n"
            + "end\n"
            + "\n"
            + "dlmwrite('matrizA1.txt',A1)\n"
            + "dlmwrite('matrizA2.txt',A2)\n"
            + "dlmwrite('size1.txt',size1)\n"
            + "dlmwrite('size2.txt',size2)\n"
            + "dlmwrite('dosed.txt',dosed)\n"
            + "dlmwrite('contNO.txt',cont_NO)\n";
    private String commands_part31 = ""
            + "if size1 == size2\n"
            + "        for i = 1 : size1( 1 )\n"
            + "            for j = 1 : size1( 2 )\n"
            + "                for k = 1 : size1( 1 )\n"
            + "                    for l = 1 : size1( 2 )\n"
            + "                        r2 = ( i - k )^2 + (j - l) ^2 ;         % distancia (radio) al cuadrado\n"
            + "                        d2 = ( A1( i , j ) - A2( k , l ) )^2 ;  % diferencia al cuadrado\n"
            + "                        Ga( k , l ) = sqrt(r2 / (DTA^2) + d2/ dosed ^ 2);\n"
            + "                    end\n"
            + "                end\n"
            + "                G( i , j )=min(min(Ga)) ;\n"
            + "            end\n"
            + "        end\n"
            + "    else\n"
            + "        fprintf=('matrices A1 y A2 no tienen la misma dimension!')\n"
            + "    end\n";
    String commands_part4 = ""
            + "');\n"
            + "G = load('matrizG.txt')\n"
            + "A1 = load('matrizA1.txt')\n"
            + "A2 = load('matrizA2.txt')\n"
            + "cont_NO = load('contNO.txt')\n"
            + "figure()\n"
            + "imshow(A1,[])\n"
            + "title('Calculo Independiente')\n"
            + "colormap(jet)\n"
            + "print -djpeg -r300 ";
    String commands_part5 = "" + "_dosis_Calc\n"
            + "\n"
            + "figure()\n"
            + "imshow(A2,[])\n"
            + "title('Dosis del TPS')\n"
            + "colormap(jet)\n"
            + "print -djpeg -r300 ";
    String commands_part6 = "" + "_dosis_TPS\n"
            + "\n"
            + "figure()\n"
            + "imshow(G,[])\n"
            + "title('Indice Gamma')\n"
            + "colormap(jet)\n"
            + "print -djpeg -r300\" ";
    String commands_part7 = "" + "_indice_Gamma\n"
            + "\n"
            + "num_puntos_aux = size(G);\n"
            + "num_puntos     = num_puntos_aux(1)*num_puntos_aux(2)-cont_NO;\n"
            + "falla          = 0;\n"
            + "\n"
            + "for i=1:num_puntos_aux(1),\n"
            + "    for j=1:num_puntos_aux(2),\n"
            + "          if (G(i,j)>1.18),\n"
            + "            falla = falla + 1;\n"
            + "        end\n"
            + "    end\n"
            + "end\n"
            + "\n"
            + "porcentaje_ok  = (num_puntos-falla)*100/num_puntos;\n"
            + "\n"
            + //"dlmwrite('Gjava3.txt',G)\n"+        
            "dlmwrite('salida.txt', [num_puntos, porcentaje_ok],'delimiter',' ','newline','pc')\n";

    public void usarMatlab(String ruta, String nombreArchivo, javax.swing.JFrame padre) throws MatlabConnectionException, MatlabInvocationException {

        String nombre = nombreArchivo.replace(".txt", "_Gamma.txt");

        MatlabProxyFactory factory = new MatlabProxyFactory();
        MatlabProxy proxy = null;
        try {
            proxy = factory.getProxy();
            //System.out.println(commands_part1 + ruta + "\\" + commands_part2 + nombre + commands_part3);
        } catch (MatlabConnectionException ex) {
            Logger.getLogger(MatLabComparacion.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
        try {
            proxy.eval(commands_part1 + ruta + "\\" + commands_part2 + nombre + commands_part3);// +
        } catch (MatlabInvocationException ex) {
            Logger.getLogger(MatLabComparacion.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
        try {
            proxy.exit();
        } catch (MatlabInvocationException ex) {
            Logger.getLogger(MatLabComparacion.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }

    }

    public void usarMatlab2(String ruta, String nombreArchivo, javax.swing.JFrame padre) throws MatlabConnectionException, MatlabInvocationException {

        String nombre = nombreArchivo.replace(".txt", "_Gamma.txt");

        MatlabProxyFactory factory = new MatlabProxyFactory();
        MatlabProxy proxy = null;
        try {
            proxy = factory.getProxy();
        } catch (MatlabConnectionException ex) {
            Logger.getLogger(MatLabComparacion.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
        try {
            //System.out.println(commands_part1 + ruta + "\\" + commands_part2 + nombre + commands_part4 + (nombreArchivo.substring(0, nombreArchivo.length() - 4)) + commands_part5 + (nombreArchivo.substring(0, nombreArchivo.length() - 4))
            //        + commands_part6 + (nombreArchivo.substring(0, nombreArchivo.length() - 4)) + commands_part7);
            proxy.eval(commands_part1 + ruta + "\\" + commands_part2 + nombre + commands_part4
                    + (nombreArchivo.substring(0, nombreArchivo.length() - 4)) + commands_part5
                    + (nombreArchivo.substring(0, nombreArchivo.length() - 4))
                    + commands_part6 + (nombreArchivo.substring(0, nombreArchivo.length() - 4)) + commands_part7);
        } catch (MatlabInvocationException ex) {
            Logger.getLogger(MatLabComparacion.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
        try {
            proxy.exit();
        } catch (MatlabInvocationException ex) {
            Logger.getLogger(MatLabComparacion.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }

    }
}
